﻿using System;
using Nancy;

namespace GeometryServer.Modules
{
    //http://localhost:13775/rest/services/Geometry/GeometryServer/AreasAndLengths?sr=102009&polygons=[{"rings":[[[-628833.344099998,206205.236200001],[-630269.659900002,192298.906100001],[-631848.233800001,173991.394400001],[-616471.690300003,341822.557500001],[-620213.661300004,301450.162799999],[-625923.431999996,237538.0579],[-628833.344099998,206205.236200001]]]}]&lengthUnit=9035&areaUnit={"areaUnit":"Acres"}&f=json
    public class AreasAndLengthsModule : NancyModule
    {
        public AreasAndLengthsModule()
            : base("/rest/services/Geometry/GeometryServer/")
        {
            Get["/AreasAndLengths"] = parameters =>
            {
                var model = new Models.AreasAndLengths();

                var exception = new GISServer.Core.Exception();
                exception.Error = new GISServer.Core.Error
                {
                    Code = 400,
                    Message = "Unable to complete areas and length operation."
                };

                #region Parameter

                if (Request.Query["f"].Value == "json")
                {
                    model.Format = Request.Query["f"].Value;
                }
                else
                {
                    model.Format = "HTML";
                }


                if (Request.Query["polygons"].Value != null)
                {
                    model.Polygons = Request.Query["polygons"].Value;
                }

                if (Request.Query["sr"].Value != null)
                {
                    int value;
                    var successful = int.TryParse(Request.Query["sr"].Value, out value);
                    if (successful)
                    {
                        model.SpatialReference = value;
                    }
                    else
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("'sr' parameter is invalid");
                    }
                }

                if (Request.Query["lengthUnit"].Value != null)
                {
                    int value;
                    var successful = int.TryParse(Request.Query["lengthUnit"].Value, out value);
                    if (successful)
                    {
                        model.LengthUnit = value;
                    }
                    else
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("'lengthUnit' parameter is invalid");
                    }

                }

                if (Request.Query["areaUnit"].Value != null)
                {
                    model.AreaUnit = Request.Query["areaUnit"].Value;
                }


                #endregion

                var output_areas_lengths = new GISServer.Core.Client.GeometryService.AreasAndLengths();

                if ((model.Polygons != null) && (!model.HasException))
                {
                    try
                    {
                        var inputpolygon = GISServer.Core.Client.Utilities.Parser.getServerPolygonArray(model.Polygons);
                        var dotspatial_polygon_array = Services.Utilities.getDotSpatialPolygonArray(inputpolygon);
                        output_areas_lengths = Services.Compute.AreasAndLengths(dotspatial_polygon_array);
                    }
                    catch (Exception)
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("Unable to calculate areas and length the geometry");
                    }
                }

                if (!model.HasException)
                {
                    model.Result = output_areas_lengths.ToJson();
                }
                else
                {
                    model.Exception = exception.ToJson();
                }


                if (model.Format.Equals("json"))
                {
                    if (!model.HasException)
                    {
                        return Services.Utilities.GetJsonResponseObject(model.Result);
                    }
                    else
                    {
                        return Services.Utilities.GetJsonResponseObject(model.Exception);
                    }
                }
                else
                {
                    return View["AreasAndLengths", model];
                }
            };

        }
    }
}